package tests.read;

import static org.junit.Assert.*;

import java.net.UnknownHostException;
import java.util.ArrayList;

import model.Budget;
import model.BudgetFactory;
import model.Line;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

import cc.test.bridge.BridgeConstants.Side;
import cc.test.bridge.LineInterface;

public class FetchLinesTest00
{
	Budget budget;
	BudgetFactory factory;
	Line line1;
	Line line2;

	@Before
	public void setUp() throws Exception
	{
		/*
		factory = new BudgetFactory();
		budget = (Budget) factory.create();
        budget.setName("Test budget");
        budget.setDescription("This is a test budget.");
        line1 = budget.createLine();
        line1.setName("Test Line 1");
		budget.add(line1);
		line2 = budget.createLine();
		line2.setName("Test Line 2");
		budget.add(line2);
		line1.commit();
		line2.commit();
		budget.commit();*/
        
	}

	@After
	public void tearDown() throws Exception
	{
		System.out.println("Tear Down");
		Mongo mongo = null;
  		try
  		{
  			mongo = new Mongo();
  		} catch (UnknownHostException e)
  		{
  			e.printStackTrace();
  		} catch (MongoException e)
  		{
  			e.printStackTrace();
  		}
  		
          DB db = mongo.getDB("cc");
          DBCollection collection = db.getCollection(budget.getRepositoryName());
          collection.remove(new BasicDBObject("entry.id", budget.getId()));
          
          collection = db.getCollection(line1.getRepositoryName());
          collection.remove(new BasicDBObject("entry.id", line1.getId()));
          collection.remove(new BasicDBObject("entry.id", line2.getId()));
	}

	@Test
	public void test()
	{
		System.out.println("testing");
		
		
		factory = new BudgetFactory();
		budget = (Budget) factory.create();
        budget.setName("Test budget");
        budget.setDescription("This is a test budget.");
        line1 = (Line) budget.createLine();
        line1.setName("Test Line 1");
        line1.commit();
		budget.add(line1);
		line2 = (Line) budget.createLine();
		line2.setName("Test Line 2");
		line2.commit();
		budget.add(line2);
		budget.commit();
		
		
		
		try
		{
			ArrayList<LineInterface> lines = budget.fetchLines(Side.EXPENDITURE);
			
			if(lines.size() != 0)
			{
				if(!lines.get(0).getName().equals("Test Line 1") || !lines.get(1).getName().equals("Test Line 2")){
					fail("Fetching all lines failed");
				}
			}
			else
				fail("Fetching all lines failed");
		}
		catch(Exception e) {
			fail("Fetching all lines failed with exception "+e);
		}
		
	}

}
